package com.hong.qrpc.codec.impl;

import com.caucho.hessian.io.Hessian2Input;
import com.hong.qrpc.codec.Decoder;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @Author HQT
 * @Date 2022/8/4 9:30
 */
@Slf4j
public class HessianDecoder implements Decoder {
    @Override
    public <T> T decode(byte[] data, Class<T> clazz) {
        T javaBean = null;
        Hessian2Input hi = null;
        ByteArrayInputStream bais = null;

        try {
            bais = new ByteArrayInputStream(data);
            hi = new Hessian2Input(bais);
            javaBean = (T) hi.readObject();
            return javaBean;
        } catch (Exception ex) {
            throw new IllegalArgumentException("HessianUtils.deserialize.异常.", ex);
        } finally {
            if (null != hi) {
                try {
                    hi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
